/*
 * @Author: Lauxb
 * @Date: 2021-04-01 11:03:28
 * @LastEditTime: 2021-11-30 15:05:45
 * @LastEditors: Please set LastEditors
 * @Description: 通用工具
 */
import store from "@/store";
import { radianToC3 } from "@tys/chaos/coordinate/convert";
import { createCartesian3 } from "@tys/chaos/coordinate";
import { getChaos } from "@/utils/sceneUtils/chaos";

/**
 * @description:  还原相机位置和视角
 * @param {*} manager
 * @param {*} duration
 * @return {*}
 */
export const updateCamera = (manager, duration = 0) => {
  const sceneMode = store.getters["scene/sceneMode"];
  let camera = null;
  if (sceneMode === "3D") {
    camera = store.getters["scene/curcamara"];
  } else {
    camera = store.getters["scene/cur2dcamara"];
  }
  const pos = createCartesian3(manager, camera.x, camera.y, camera.z);
  const params = {
    destination: pos,
    duration: duration
  };
  if (
    camera &&
    camera.heading !== undefined &&
    camera.pitch !== undefined &&
    camera.roll !== undefined
  ) {
    params.orientation = {
      heading: camera.heading,
      pitch: camera.pitch,
      roll: camera.roll
    };
  }
  if (
    camera &&
    camera.upWCX !== undefined &&
    camera.upWCY !== undefined &&
    camera.upWCZ !== undefined
  ) {
    params.up = createCartesian3(
      manager,
      camera.upWCX,
      camera.upWCY,
      camera.upWCZ
    );
  }
  if (
    camera &&
    camera.directionX !== undefined &&
    camera.directionY !== undefined &&
    camera.directionZ !== undefined
  ) {
    params.direction = createCartesian3(
      manager,
      camera.directionX,
      camera.directionY,
      camera.directionZ
    );
  }
  manager.viewer.camera.flyTo(params);
};
/**
 * @description: 记录此时相机位置和视角
 * @param {*} manager
 * @return {*}
 */
export const recordCamera = manager => {
  const sceneMode = store.getters["scene/sceneMode"];
  const camera = manager.viewer.camera;
  const {
    heading,
    pitch,
    roll,
    position,
    positionCartographic,
    upWC,
    direction
  } = camera;
  if (sceneMode === "3D") {
    const params = {
      x: position.x,
      y: position.y,
      z: position.z,
      heading: heading,
      pitch: pitch,
      roll: roll,
      upWCX: upWC.x,
      upWCY: upWC.y,
      upWCZ: upWC.z,
      directionX: direction.x,
      directionY: direction.y,
      directionZ: direction.z
    };
    store.dispatch("scene/setCurCamara", params);
  } else {
    const pos = radianToC3(
      manager,
      positionCartographic.longitude,
      positionCartographic.latitude,
      positionCartographic.height
    );
    const params = {
      x: pos.x,
      y: pos.y,
      z: pos.z
    };
    store.dispatch("scene/setCur2DCamara", params);
  }
};

const chaos = getChaos();
// 定位视点
export const locationVisual = tempEntities => {
  const cartesian = tempEntities[0].position._value;
  const endPoint = tempEntities[1].position._value;

  const heading = getHeading(cartesian, endPoint);
  const pitch = getPitch(cartesian, endPoint);

  chaos.viewer.camera.flyTo({
    destination: cartesian,
    orientation: {
      heading,
      pitch,
      roll: 0
    },
    duration: 1.0,
    complete() {
      // chaos.viewer.scene.camera.moveBackward(20);
    }
  });
};

const getHeading = (pointA, pointB) => {
  //建立以点A为原点，X轴为east,Y轴为north,Z轴朝上的坐标系
  const transform = chaos.cesium.Transforms.eastNorthUpToFixedFrame(pointA);
  //向量AB
  const positionvector = chaos.cesium.Cartesian3.subtract(
    pointB,
    pointA,
    new chaos.cesium.Cartesian3()
  );
  //因transform是将A为原点的eastNorthUp坐标系中的点转换到世界坐标系的矩阵
  //AB为世界坐标中的向量
  //因此将AB向量转换为A原点坐标系中的向量，需乘以transform的逆矩阵。
  const vector = chaos.cesium.Matrix4.multiplyByPointAsVector(
    chaos.cesium.Matrix4.inverse(transform, new chaos.cesium.Matrix4()),
    positionvector,
    new chaos.cesium.Cartesian3()
  );
  //归一化
  const direction = chaos.cesium.Cartesian3.normalize(
    vector,
    new chaos.cesium.Cartesian3()
  );
  //heading
  const heading =
    Math.atan2(direction.y, direction.x) - chaos.cesium.Math.PI_OVER_TWO;
  return chaos.cesium.Math.TWO_PI - chaos.cesium.Math.zeroToTwoPi(heading);
};
const getPitch = (pointA, pointB) => {
  const transfrom = chaos.cesium.Transforms.eastNorthUpToFixedFrame(pointA);
  const vector = chaos.cesium.Cartesian3.subtract(
    pointB,
    pointA,
    new chaos.cesium.Cartesian3()
  );
  const direction = chaos.cesium.Matrix4.multiplyByPointAsVector(
    chaos.cesium.Matrix4.inverse(transfrom, transfrom),
    vector,
    vector
  );
  chaos.cesium.Cartesian3.normalize(direction, direction);
  //因为direction已归一化，斜边长度等于1，所以余弦函数等于direction.z
  return (
    chaos.cesium.Math.PI_OVER_TWO - chaos.cesium.Math.acosClamped(direction.z)
  );
};
